package com.vip.base.controller;


import com.vip.base.service.ISysUserService;
import com.vip.common.annotation.LogCollection;
import com.vip.common.constant.LogConsts;
import com.vip.common.constant.UrlConsts;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.RestResponse;
import com.vip.common.response.RestResponses;
import com.vip.common.response.Result;
import com.vip.database.dto.user.AddUserDTO;
import com.vip.database.dto.user.GetUserDTO;
import com.vip.oauth2resource.util.CustomUserDetail;
import com.vip.oauth2resource.util.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户表前端控制器
 *
 * @author LEON
 * @since 2020-03-28
 */
@RestController
@RequestMapping(UrlConsts.API + "/user")
public class SysUserController {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取当前用户
     *
     * @return
     */
    @GetMapping("/curr")
    public RestResponse<CustomUserDetail> getCurrUser() {
        return RestResponses.newSuccessResponse(UserHelper.getUser());
    }

    /**
     * 分页获取用户列表数据
     *
     * @param pageNo   当前页
     * @param pageSize 当前页大小
     * @return 当前页列表数据
     */
    @GetMapping(value = "/list")
    public RestResponse<List<GetUserDTO>> getUserList(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam(required = false) String account, @RequestParam(required = false) String name) {
        return RestResponses.newResponseFromResult(sysUserService.getUserList(pageNo, pageSize, account, name));
    }

    /**
     * 获取指定用户详情
     *
     * @param id
     */
    @GetMapping(value = "/{id}")
    public RestResponse<GetUserDTO> getUserDetails(@PathVariable String id) {
        return RestResponses.newResponseFromResult(sysUserService.getUserDetails(id));
    }

    /**
     * 用户授权
     *
     * @return
     */
    @LogCollection(object = LogConsts.OBJ_USER)
    @PutMapping(value = "/accredit/{userId}")
    public RestResponse<String> userAccredit(@PathVariable String userId, @RequestParam String roleId, @RequestParam Boolean isAdd) {
        return RestResponses.newResponseFromResult(sysUserService.userAccredit(roleId, userId, isAdd));
    }

    /**
     * 添加新用户
     *
     * @param dto
     * @return
     */
    @LogCollection(object = LogConsts.OBJ_USER)
    @PostMapping
    public RestResponse<String> addNewUser(@RequestBody @Valid AddUserDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResponses.newFailResponse(ErrorCode.INVALID_PARAMETER, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Result<Boolean> isExistResult =  sysUserService.userIsExist(dto.getAccount());
        if(!isExistResult.getData()){
            return RestResponses.newResponseFromResult(sysUserService.addNewUser(dto));
        }
        return RestResponses.newFailResponse(ErrorCode.LOGIC_OBEY, isExistResult.getDescription());
    }

    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @LogCollection(object = LogConsts.OBJ_USER)
    @DeleteMapping("/{id}")
    public RestResponse<String> deleteUser(@PathVariable("id") String id) {
        return RestResponses.newResponseFromResult(sysUserService.deleteUserById(id));
    }

    /**
     * 修改用户信息
     *
     * @param dto
     * @return
     */
    @LogCollection(object = LogConsts.OBJ_USER)
    @PutMapping("/{id}")
    public RestResponse<GetUserDTO> updateUser(@PathVariable String id, @RequestBody @Valid AddUserDTO dto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResponses.newFailResponse(ErrorCode.INVALID_PARAMETER, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RestResponses.newResponseFromResult(sysUserService.updateUser(id, dto));
    }

}

